package net.goutalk.glcs.module.system.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.system.dto.AddSubsystemDto;
import net.goutalk.glcs.module.system.dto.SubsystemListDto;
import net.goutalk.glcs.module.system.dto.SubsystemPageDto;
import net.goutalk.glcs.module.system.dto.UpdateSubsystemDto;
import net.goutalk.glcs.module.system.entity.Subsystem;
import net.goutalk.glcs.module.system.service.ISubsystemService;
import net.goutalk.glcs.module.system.vo.SubsystemListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 子系统 前端控制器
 * </p>
 *
 * @author zjq
 * @since 2022-10-08
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX + "/subsystem")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/subsystem", tags = "子系统管理")
@AllArgsConstructor
public class SubsystemController {
    private final ISubsystemService subsystemService;

    @GetMapping("/list")
    @ApiOperation("查询子系统表列表数据(不分页)")
    public R list(@Valid SubsystemListDto dto) {
        LambdaQueryWrapper<Subsystem> wrapper = Wrappers.<Subsystem>query().lambda()
                .like(StrUtil.isNotBlank(dto.getKeyword()), Subsystem::getName, dto.getKeyword())
                .last(StringUtils.isNotEmpty(dto.getField()), GlobalConstant.ORDER_BY + StringPool.SPACE + StrUtil.toUnderlineCase(dto.getField()) + StringPool.SPACE + ConventPage.getOrder(dto.getOrder()))
                .select(Subsystem.class, x -> VoToColumnUtil.fieldsToColumns(SubsystemListVo.class).contains(x.getProperty()))
                .orderByAsc(Subsystem::getSortCode);
        List<SubsystemListVo> subsystemListVos = BeanUtil.copyToList(subsystemService.list(wrapper), SubsystemListVo.class);
        return R.ok(subsystemListVos);
    }

    @GetMapping("/page")
    @ApiOperation("查询子系统表列表数据(分页)")
    public R page(@Valid SubsystemPageDto dto) {
        LambdaQueryWrapper<Subsystem> wrapper = Wrappers.<Subsystem>query().lambda()
                .like(StrUtil.isNotBlank(dto.getKeyword()), Subsystem::getName, dto.getKeyword())
                .last(StringUtils.isNotEmpty(dto.getField()), GlobalConstant.ORDER_BY + StringPool.SPACE + StrUtil.toUnderlineCase(dto.getField()) + StringPool.SPACE + ConventPage.getOrder(dto.getOrder()))
                .select(Subsystem.class, x -> VoToColumnUtil.fieldsToColumns(SubsystemListVo.class).contains(x.getProperty()))
                .orderByAsc(Subsystem::getSortCode);
        IPage<Subsystem> page = subsystemService.page(ConventPage.getPage(dto), wrapper);
        return R.ok(ConventPage.getPageOutput(page, SubsystemListVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增子系统")
    public R add(@Valid @RequestBody AddSubsystemDto dto) {
        Subsystem subsystem = BeanUtil.toBean(dto, Subsystem.class);
        return R.ok(subsystemService.save(subsystem));
    }

    @PutMapping
    @ApiOperation(value = "更新子系统")
    public R update(@Valid @RequestBody UpdateSubsystemDto dto){
        Subsystem subsystem = BeanUtil.toBean(dto, Subsystem.class);
        return R.ok(subsystemService.updateById(subsystem));
    }

//TODo:  用户授权相关表glcs_base_relation glcs_base_Authorize
    @GetMapping
    @ApiOperation("查询当前用户授权的子系统")
    public R AuthList(){

        return R.ok();
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@RequestBody List<Long> ids) {
        if (ids.contains(1L)) {
            return R.error("不能删除主系统！");
        }
        return R.ok(subsystemService.removeBatchByIds(ids));
    }

}
